const api = require('../../../ws_micro_shop/utils/api.js');
const app = getApp();

Page({
  data: {
    theme: app.themeData,
  },
  onLoad() {
    let that = this;
    app.loadCurrencyUnit().then(res => {
      that.setData({
        currency: res.currency,
      });
    });
    app.loadShopSetting().then(res => {
      that.setData({
        check_mobile: res.check_mobile,
      });
      wx.setNavigationBarTitle({
        title: res.alias + '信息编辑',
      });
    });
    app.ws_micro_shoprequest({
      url: api.share.save_info,
      method: 'GET',
      success: function (res) {
        if (res.code === 0) {
          that.setData({
            info: res.data,
          });
        }
      },
    });
  },
  formSubmit(e) {
    let formData = e.detail.value;
    let name = formData.u_name;
    let mobile = formData.u_phone;
    if (Number(this.data.check_mobile) === 1) {
      if (this.data.currency === 'CNY') {
        if (!app.util.phoneExp.mobile.test(mobile)) {
          wx.showToast({
            title: '手机号码格式不正确！',
            icon: 'none',
          });
          return;
        }
      } else {
        if (!app.util.phoneExp.canadaPhone.test(mobile)) {
          wx.showToast({
            title: '手机号码格式不正确！',
            icon: 'none',
          });
          return;
        }
      }
    }
    app.ws_micro_shoprequest({
      url: api.share.save_info,
      data: {
        name,
        mobile,
      },
      method: 'POST',
      success: function (res) {
        if (res.code === 0) {
          wx.showModal({
            title: '提示',
            content: '保存成功',
            showCancel: false,
            success: function (res) {
              if (res.confirm) {
                wx.navigateBack();
              }
            },
          });
        } else {
          wx.showToast({
            title: res.msg,
          });
        }
      },
    });
  },
});
